/*
 * @Author: 墨凡 oncwnuN3VU3V55c1lqO_6llf5DtU@git.weixin.qq.com
 * @Date: 2023-02-04 08:23:31
 * @LastEditors: 墨凡 oncwnuN3VU3V55c1lqO_6llf5DtU@git.weixin.qq.com
 * @LastEditTime: 2023-02-27 15:54:18
 * @FilePath: \borun-admin\src\pinia\modules\baseInfo.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from 'pinia'
import { getMediumList } from '@/api/medium'
import { getCarList } from '@/api/car'
import { getTrailerList } from '@/api/trailer'
import { getMemberList } from '@/api/member'
import { getSupercargoList } from '@/api/supercargo'
import { getCustomerList } from '@/api/customer'

export const useBaseStore = defineStore('baseInfo', {
  state: () => ({
    medium_name: [],
    vehicle_no: [],
    trailer_no: [],
    customer_name: [],
    driver_name: [],
    supercargo_name: [],
  }),
  // 类似于组件的computed，用来封装计算属性，具有缓存的功能
  getters: {
    // 函数接收一个可选参数：state状态对象
  },
  actions: {
    // 获取运输介质资料
    async getMediumData() {
      const { code, data } = await getMediumList()
      if (+code === 0) {
        const medium = data.list.map(res => {
          return {
            label: res.medium_name,
            value: res.medium_id,
            disabled: +res.status === 1 ? false : true,
          }
        })
        this.medium_name = medium
        return Promise.resolve(medium)
      }
    },
    // 获取车牌号
    async getVehicleData() {
      const { code, data } = await getCarList({
        page: 1,
        offset: 9999,
      })
      if (+code === 0) {
        const vehicle = data.list.map(res => {
          return {
            label: res.car_no,
            value: res.id,
            disabled: +res.status === 1 ? false : true,
          }
        })
        this.vehicle_no = vehicle
        return Promise.resolve(vehicle)
      }
    },
    // 获取挂车拍照
    async getTrailerData() {
      const { code, data } = await getTrailerList({
        page: 1,
        offset: 9999,
      })
      if (+code === 0) {
        const trailer = data.list.map(res => {
          return {
            label: res.car_cg_no,
            value: res.id,
            disabled: +res.status === 1 ? false : true,
          }
        })
        this.trailer_no = trailer
        return Promise.resolve(trailer)
      }
    },
    // 获取驾驶员
    async getDriverData() {
      const { code, data } = await getMemberList({
        page: 1,
        offset: 9999,
      })
      if (+code === 0) {
        const driver = data.list.map(res => {
          return {
            label: res.username,
            value: res.uid,
            disabled: +res.status === 1 ? false : true,
          }
        })
        this.driver_name = driver
        return Promise.resolve(driver)
      }
    },
    // 获取押运员
    async getSupercargoData() {
      const { code, data } = await getSupercargoList({
        page: 1,
        offset: 9999,
      })
      if (+code === 0) {
        const supercargo = data.list.map(res => {
          return {
            label: res.supercargo_name,
            value: res.supercargo_id,
            disabled: +res.status === 1 ? false : true,
          }
        })
        this.supercargo_name = supercargo
        return Promise.resolve(supercargo)
      }
    },
    // 获取客户信息，委托公司与运达公司
    async getCustomerData() {
      const { code, data } = await getCustomerList({
        page: 1,
        offset: 9999,
      })
      if (+code === 0) {
        const customer = data.list.map(res => {
          return {
            label: res.customer_name,
            value: res.customer_id,
            disabled: +res.status === 1 ? false : true,
          }
        })
        this.customer_name = customer
        return Promise.resolve(customer)
      }
    },
  },
})
